//
//  AppMyCenterController.m
//  gungunchou
//
//  Created by lyzbmacos3 on 2019/1/10.
//  Copyright © 2019年 xskj. All rights reserved.
//

#import "AppMyCenterController.h"
#import "LoginController.h"
#import "LJWebKitSupport.h"

@interface AppMyCenterController () <WKNavigationDelegate>
@property (nonatomic, copy) NSString *UrlString;
@property (nonatomic, strong) WKWebView *wkWeb;
@property (nonatomic, assign) BOOL isLoginHandled;
@end

@implementation AppMyCenterController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self setupViews];
}

- (void)setupViews
{
    self.UrlString = Usr_Is_L?Center_Url_2:Center_Url_1;
    self.wkWeb = [[WKWebView alloc] initWithFrame:CGRectZero configuration:[LJWebKitSupport sharedSupport].webViewConfiguration];
    WS(weakSelf);
    self.wkWeb.navigationDelegate = weakSelf;
    self.wkWeb.scrollView.showsVerticalScrollIndicator = NO;
    self.wkWeb.scrollView.showsHorizontalScrollIndicator = NO;
    self.wkWeb.scrollView.scrollEnabled = NO;
    [self.view addSubview:self.wkWeb];
    [self.wkWeb mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.wkWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.UrlString]]];
    
    [[CGLodingView shareInstance] startAnimation];//第一次进来在启动页后面loading
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    if (Usr_Is_L && !self.isLoginHandled) {
        self.isLoginHandled = YES;
        [self setupViews];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (Usr_Is_L) {
        
    }
    else {
        LoginController *logCon = LoginController.new;
        [self.navigationController presentViewController:logCon animated:YES completion:nil];
    }
}


#pragma mark ======= WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    
}

// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
    
}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    NSString *url = webView.URL.absoluteString;
    NSLog(@"网页加载完成");
    [[CGLodingView shareInstance] stopAnimation];
    if ([url containsString:@"Member/PersonalCenter"]) {
        [self listenLogoutJS];
    }
}

// 在发送请求之前，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSString *url = [navigationAction.request.URL absoluteString];
    
    //UIWebView打开一个页面之后，点击里面的内容就直接能跳转。而WKWebView一开始点里面的东西没反应，只要加了这个方法就好了
    if (navigationAction.targetFrame == nil) {
        [webView loadRequest:navigationAction.request];
    }
    
    if ([LJUtility theString:url containSting:@"ios_jsapi"]) {
        if ([url containsString:@"ios_jsapi/logout"]) {
            //退出登录处理
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"USR_IS_L"];
            self.tabBarController.selectedIndex = 0;
        }
        decisionHandler(WKNavigationActionPolicyCancel);
    }
    else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

- (void)listenLogoutJS
{
    NSString *js = @"$(\".fb-btn\").hide();";//@"$(\"#btnLogout\").click();";
    [self loadJsWithJs:js];
}

- (void)loadJsWithJs:(NSString *)js
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.wkWeb evaluateJavaScript:js completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            [[CGLodingView shareInstance] stopAnimation];
            if (error) {
                NSLog(@"wkWeb evaluateJavaScript error = %@\n", error);
            }
        }];
    });
}
@end
